package com.lxhll.personnel.controller.transfer;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxhll.personnel.core.ResponseData;
import com.lxhll.personnel.entity.Employee;
import com.lxhll.personnel.entity.PositionTransfer;
import com.lxhll.personnel.service.EmployeeService;
import com.lxhll.personnel.service.PositionService;
import com.lxhll.personnel.service.PositionTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * (PositionTransfer)表控制层 员工岗位调动
 *仅需要对员工更改部门，并记录信息至表中
 * @author liangyi
 * @since 2021-07-22 14:52:37
 */
@RestController
@RequestMapping("/position_transfer")
public class PositionTransferController {
    /**
     * 服务对象
     */
    @Autowired
    private PositionTransferService transferService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private PositionService positionService;

           
      /**
     * 查看以调动员工岗位调动信息
     */
    @PostMapping("/list")
    public ResponseData list(@RequestBody Map<String,Object> params){
        IPage<Map<String,Object>> page=transferService.queryTransfers(params);
        return ResponseData.ok(page);
    }
    
    @GetMapping("/info/{id}")
    public ResponseData info(@PathVariable Integer id){
        return ResponseData.ok(transferService.getTransferById(id));
    }

    @PostMapping("/save")
    public ResponseData save(@RequestBody PositionTransfer transfer){
         //判断当前岗位是否超员
        //新岗位的总员工<新岗位规定的员工
        int totalNum=employeeService.count(new QueryWrapper<Employee>().eq("position_id",transfer.getNewPositionId()));
        int maxNum=positionService.getById(transfer.getNewPositionId()).getMaxNum();
        if(totalNum>=maxNum){
            return ResponseData.error("该岗位已满编！");
        }
        //获取部门调转信息
        //设置调转时间,插入调动信息
        if(transferService.saveTransfer(transfer)){
             return ResponseData.ok();
        }
        return ResponseData.error();
    }
}
